const app = getApp();
let {
  $resHost
} = app.globalData;
// pages/Employee/Activity/activity.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    linkImg: `${$resHost}/img/jzbjwx.png`,
    page: 1,
    total: 0,
    isMore: true,
    list: [],
    houseTagId: '',
    styleTagId: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.login().then(() => {
      this.getTag('houseTypeList', 2);
      this.getTag('classList', 1);
      this.setData({
        orgId: xhs.getStorageSync('config').topOrgId
      }, () => {
        this.getList();
      });
    });
  },
  selectFN(e) {
    let {
      id,
      type
    } = e.currentTarget.dataset;
    this.setData({
      [type]: id
    }, () => {
      this.getList();
    });
  },
  getList() {
    app.$getData('/user/case/case-list', {
      currentPage: 1,
      pageSize: 10,
      orgId: this.data.orgId,
      styleTagId: this.data.styleTagId,
      houseTagId: this.data.houseTagId,
      employeeId: this.data.employeeId
    }).then(data => {
      if (data.errCode == 0) {
        this.setData({
          page: 1,
          isMore: true
        });
        this.data.total = data.count;
        data = data.data;
        xhs.stopPullDownRefresh();
        this.setData({
          list: data,
          total: this.data.total,
          page: this.data.page + 1,
          isMore: false
        });
      }
    });
  },
  getUserInfo() {
    xhs.getStorage({
      key: 'employeeInfo',
      success: res => {
        this.setData({
          userInfo: res.data
        });
      }
    });
  },
  getTag(type, typeId) {
    app.$getData('/user/case/tag-list', {
      typeId: typeId
    }).then(data => {
      if (data.errCode == 0) {
        data.data.unshift({
          id: '',
          name: '全部'
        });
        this.setData({
          [type]: data.data
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 自定义导航
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.list.length >= this.data.total) {
      return this.setData({
        isMore: false
      });
    }
    xhs.showLoading({
      title: ' 加载中'
    });
    app.$getData('/user/case/case-list', {
      currentPage: this.data.page,
      pageSize: 10,
      orgId: this.data.orgId,
      styleTagId: this.data.styleTagId,
      houseTagId: this.data.houseTagId
    }).then(data => {
      if (data.errCode == 0) {
        data = data.data;
        xhs.hideLoading();
        xhs.stopPullDownRefresh();
        this.setData({
          list: this.data.list.concat(data),
          page: this.data.page + 1
        });
      }
    });
  }
});